<?php
App::uses('AppController', 'Controller');
/**
 * Groups Controller
 *
 * @property Group $Group
 */
class GroupsController extends AppController {

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Group->recursive = 0;
		$this->set('groups', $this->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		$this->Group->id = $id;
		if (!$this->Group->exists()) {
			throw new NotFoundException(__('Invalid group'));
		}
		$this->set('group', $this->Group->read(null, $id));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		$states = $this->Group->State->find('list');
		$this->set(compact('states'));
		if ($this->request->is('post')) {
			$this->Group->create();
			if ($this->Group->save($this->request->data)) {
				$this->Session->setFlash(__('The group has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The group could not be saved. Please, try again.'));
			}
		}
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		$this->Group->id = $id;
		
		$this->set('perms', $this->GetAcos($id));
		
		if (!$this->Group->exists()) {
			throw new NotFoundException(__('Invalid group'));
		}
		$states = $this->Group->State->find('list');
		$this->set(compact('states'));
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Group->save($this->request->data)) {
				$this->Session->setFlash(__('The group has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The group could not be saved. Please, try again.'));
			}
		} else {
			$this->request->data = $this->Group->read(null, $id);
		}
	}

/**
 * delete method
 *
 * @throws MethodNotAllowedException
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->Group->id = $id;
		if (!$this->Group->exists()) {
			throw new NotFoundException(__('Invalid group'));
		}
		if ($this->Group->delete()) {
			$this->Session->setFlash(__('Group deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Group was not deleted'));
		$this->redirect(array('action' => 'index'));
	}
	
	public function beforeFilter() {
    parent::beforeFilter();

    // For CakePHP 2.0
    $this->Auth->allow('*');

    // For CakePHP 2.1 and up
    $this->Auth->allow();
}

private function GetAcos($groupId){
	
	$userGroup = $this->Group->findById($groupId);
	/*$aro = $this->Acl->Aro->find('first', array(
	 'conditions' => array(
	 		'Aro.model' => 'Group',
	 		'Aro.foreign_key' => $this->_user['User']['group_id'],
	 ),
	));*/
	$permissionsArray;
		
	$acos = $this->Acl->Aco->children();
	foreach($acos as $aco){
		$permissionsArray[$aco['Aco']['alias']] =	false;
		
		if(!empty($aco['parent_id'])){
			$parentAco = $this->Acl->Aco->find('first', array(
					'conditions' => array(
							'id' => $aco['Aco']['parent_id']
					)
			));
			$permissionsArray[$aco['Aco']['alias']] =	false;
		}
	}

	$aro = $this->Acl->Aro->find('first', array(
				'conditions' => array(
						'Aro.model' => 'Group',
						//'Aro.foreign_key' => $this->_user['User']['group_id'],
						'Aro.foreign_key' => $groupId,
				),
		));
		
	$acos = $this->Acl->Aco->children();
		

	
	foreach($acos as $aco){
		$permission = $this->Acl->Aro->Permission->find('first', array(
				'conditions' => array(
						'Permission.aro_id' => $aro['Aro']['id'],
						'Permission.aco_id' => $aco['Aco']['id'],
				),
		));

		if(isset($permission['Permission']['id'])){

				
			if ($permission['Permission']['_create'] == 1 ||
					$permission['Permission']['_read'] == 1 ||
					$permission['Permission']['_update'] == 1 ||
					$permission['Permission']['_delete'] == 1) {

				$permissionsArray[$permission['Aco']['alias']] =	true;
				if(!empty($permission['Aco']['parent_id'])){
					$parentAco = $this->Acl->Aco->find('first', array(
							'conditions' => array(
									'id' => $permission['Aco']['parent_id']
							)
					));
					$permissionsArray[$permission['Aco']['alias']] =	true;
				}
				
			}else {
				$permissionsArray[$permission['Aco']['alias']] =	false;
			}
				
		}
	}

	//return $permissionsArray;
	return $permissionsArray;

}

}
